package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Member;
import com.itheima.pojo.Setmeal;
import com.itheima.service.MemberService;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ForkJoinPool;

@RestController
public class MemberController {

    @Reference
    private MemberService memberService;

    // /member/findPage
    @RequestMapping("/member/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return memberService.findPage(queryPageBean);
    }

    // /member/getUsers
    @RequestMapping("/member/getUsers")
    public Result getUsers() {
        return memberService.getUsers();
    }

    // /member/add
    @RequestMapping("/member/add")
    public Result add(@RequestBody Member member) {
        return memberService.add(member);
    }

    // /member/getById
    @RequestMapping("/member/getById")
    public Result getById(Integer id) {
        return memberService.findById(id);
    }

    // /member/edit
    @RequestMapping("/member/edit")
    public Result edit(@RequestBody Member member) {
        return memberService.edit(member);
    }

    // /member/delete

    @RequestMapping("/member/delete")
    public Result delete(Integer id) {
        return memberService.delete(id);
    }

    // /member/getExcel
    @RequestMapping("/member/getExcel")
    public void getExcel(Integer[] ids, HttpServletRequest resp, HttpServletResponse response) {
        try {
            // 档案号和手机号
            String temlateRealPath = resp.getSession().getServletContext().getRealPath("template") + File.separator + "1.xlsx";
            XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File(temlateRealPath)));
            XSSFSheet sheet = workbook.getSheetAt(0);
            int index_i = 1;


            for (Integer id : ids) {
                XSSFRow row = sheet.createRow(index_i);

                Member member = memberService.getMemberById(id);

                List<Setmeal> setmealList = memberService.findByMemberId(id);
                List<String> setmealNames = new ArrayList<>();
                List<CheckGroup> checkGroupList = new ArrayList<>();
                for (Setmeal setmeal : setmealList) {
                    setmealNames.add(setmeal.getName());

                    List<CheckGroup> list = memberService.findByCheckGroup(setmeal.getId());
                    for (CheckGroup checkGroup : list) {
                        checkGroupList.add(checkGroup);
                    }

                }

                List<String> checkGroupNames = new ArrayList<>();

                List<CheckItem> checkItemList = new ArrayList<>();

                for (CheckGroup checkGroup : checkGroupList) {
                    checkGroupNames.add(checkGroup.getName());

                    List<CheckItem> list = memberService.findByCheckItem(checkGroup.getId());
                    for (CheckItem checkItem : list) {
                        checkItemList.add(checkItem);
                    }
                }

                List<String> checkItemNames = new ArrayList<>();
                for (CheckItem checkItem : checkItemList) {
                    checkItemNames.add(checkItem.getName());
                }

                String setmealString = JSON.toJSONString(setmealNames);
                String checkGroupString = JSON.toJSONString(checkGroupNames);
                String checkItemString = JSON.toJSONString(checkItemNames);



                row.createCell(0).setCellValue(member.getName());
                row.createCell(1).setCellValue(member.getSex());
                row.createCell(2).setCellValue(member.getPhoneNumber());
                row.createCell(3).setCellValue("机构地址");
                row.createCell(4).setCellValue(setmealString);
                row.createCell(5).setCellValue(checkGroupString);
                row.createCell(6).setCellValue(checkItemString);
                index_i++;
            }





            OutputStream os = response.getOutputStream();
            //  application/vnd.ms-excel 代表向客户端声明 输出的是 excel 文件
            response.setContentType("application/vnd.ms-excel");
            //指定 以附件的形式下载
            response.setHeader("content-Disposition", "attachment;filename=1.xlsx");
            //通过 excel 文件中的 write 方法将流写到前端
            workbook.write(os);

            os.flush();
            os.close();
            workbook.close();
            //return new Result(true,"导出成功");
        } catch (Exception e) {
            e.printStackTrace();
            // return new Result(false,"导出失败");
        }
    }


}
